// 发送好友消息 {"内容":"123123213","id":25}

// @ts-ignore
export async function execute(client_id, message_json) {
    let id = Number(message_json["id"]);
    let text = message_json["内容"];
    let characters = G.Characters.get_current_login_characters(client_id);
    // 初始化消息
    if (!_FriendMessageCache.has(id)) {
        _FriendMessageCache.set(id, []);
    }
    let messages = _FriendMessageCache.get(id);
    messages.push({
        "id": characters["id"],
        "名称": characters["名称"],
        "类型": 1,
        "模型": characters["模型"],
        "等级": characters["等级"],
        "好友度": 0,
        "时间": "",
        "内容": text,
    });
    // 通知发送人客户端已发送消息
    let time = formatDateTime();
    ClientUtils.send_client_message(client_id, 55, { "信息": `${characters["名称"]}   ${time}`, "id": characters["id"], "内容": text });
    // 通知接收人客户端收到新的好友消息
    ClientUtils.send_characters_client_message(id, 55, { "信息": `${characters["名称"]}   ${time}`, "id": characters["id"], "内容": text });
    ClientUtils.send_characters_client_message(id, 56, "1");
}

function formatDateTime(timestamp = Date.now()) {
  const date = new Date(timestamp);
  
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  
  let hours = date.getHours();
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}